package com.lagou.edu.service.impl;

import com.lagou.edu.service.EmailService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;

import java.util.Date;

/**
 * <p>
 *
 * </p>
 *
 * @author KK
 * @since 2021-09-24
 */
@Service
public class EmailServiceImpl implements EmailService {


    private final JavaMailSender javaMailSender;

    @Autowired
    public EmailServiceImpl(JavaMailSender javaMailSender) {
        this.javaMailSender = javaMailSender;
    }


    @Override
    public boolean mail(String email, String code) {
        try {
            // 发送邮件
            SimpleMailMessage message = new SimpleMailMessage();
            message.setSubject("这是一封测试邮件");
            message.setFrom("zhengzekaitest@163.com");
            message.setTo(email);
            message.setSubject("验证码邮件");
            message.setSentDate(new Date());
            message.setText("欢迎注册， 您的验证码为：" + code);
            javaMailSender.send(message);
        } catch (MailException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
